Module: DiasporaClient

Defined in:
lib/diaspora-client.rb,
lib/diaspora-client/app.rb,
lib/diaspora-client/railtie.rb,
lib/diaspora-client/access_token.rb,
lib/diaspora-client/resource_server.rb

Defined Under Namespace

Classes: AccessToken, App, Railtie, RegistrationError, ResourceServer

Constant Summary

PROFILE =
"profile"
PHOTOS =
"photos"
READ =
"read"
WRITE =
"write"

Class Method Summary (collapse)

Class Method Details

+ (Addressable::URI) application_base_url

Normalizes and adds a scheme and port to @application_base_url.

Returns:

  • (Addressable::URI)

    The url of the server using DiasporaClient.



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/diaspora-client.rb', line 137

def self.application_base_url
  if @application_base_url.match(/^localhost:\d+/)
    @application_base_url = DiasporaClient.scheme + "://" + @application_base_url
  end

  host = Addressable::URI.heuristic_parse(@application_base_url)
  host.scheme = self.scheme
  host.port ||= host.inferred_port
  host.path = '/' if host.path.blank?
  host
end

+ (void) config(&block)

This method returns an undefined value.

Calls initialize_instance_variables and yields to a given (optional) config block.

Examples:

DiasporaClient.config do |d|
  d.test_mode = true

  #The base url of your application.  Your manifest must be at https://[application/base/url/]manifest.json.
  d.application_base_url = "chubbi.es/"

  d.manifest_field(:name, "Chubbies")
  d.manifest_field(:description, "The best way to chub.")
  d.manifest_field(:icon_url, "#")

  d.manifest_field(:permissions_overview, "Chubbi.es wants to post photos to your stream.")

  d.permission(:profile, :read, "Chubbi.es wants to view your profile so that it can show it to other users.")
  d.permission(:photos, :write, "Chubbi.es wants to write to your photos to share your findings with your contacts.")
end


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/diaspora-client.rb', line 73

def self.config(&block)
  self.initialize_instance_variables

  if block_given?
    block.call(self)
  end

  if @test_mode
    self.set_test_defaults
  end
end

+ (Hash) generate_manifest

Generates the manifest content used in package_manifest.

Returns:

  • (Hash)


201
202
203
204
# File 'lib/diaspora-client.rb', line 201

def self.generate_manifest
  @manifest_fields.merge(:permissions => @permissions,
                         :application_base_url => self.application_base_url.to_s)
end

+ (void) getter(*fields)

This method returns an undefined value.

Sets getter field(s) for the module.

Parameters:

  • fields (Symbol, String)

    Variable to be retrieved



34
35
36
37
38
# File 'lib/diaspora-client.rb', line 34

def self.getter(*fields)
  fields.each do |f|
    eval("def self.#{f} ; @#{f} ; end")
  end
end

+ (void) initialize_instance_variables

This method returns an undefined value.

Initilizes public & private keys, permissions and manifest fields.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/diaspora-client.rb', line 152

def self.initialize_instance_variables
  app_name = (defined?(Rails)) ? "#{Rails.application.class.parent_name}." : ""
  app_root = (defined?(Rails)) ? "#{Rails.root}" : ""

  @permissions = {}
  @manifest_fields = {}

  @private_key_path = "#{app_root}/config/#{app_name}private.pem"
  @private_key = nil

  @public_key_path = "#{app_root}/config/#{app_name}public.pem"
  @public_key = nil

  @test_mode = false

  @faraday_initialized = nil
end

+ (void) manifest_field(field, value)

This method returns an undefined value.

Defines a field to be placed in the application's manifest.

Examples:

manifest_field(:description, "This application is totally bananas.")

Parameters:

  • field (Symbol)
  • value (String)


179
180
181
182
# File 'lib/diaspora-client.rb', line 179

def self.manifest_field(field, value)
  @manifest_fields[field] = value
  nil
end

+ (String) package_manifest

Generates a manifest of the form => key, :jwt => jwt

Returns:

  • (String)

    manifest The resulting manifest.json



209
210
211
212
# File 'lib/diaspora-client.rb', line 209

def self.package_manifest
  JSON.generate({:public_key => self.public_key,
                 :jwt => JWT.encode(self.generate_manifest, self.private_key, "RS512")})
end

+ (void) permission(type, access, description)

This method returns an undefined value.

Defines the permissions the applicaiton is attempting to access.

Examples:

permission(:profile, :read, "Chubbi.es wants to view your profile so that it can show it to other users.")

Parameters:

  • type (Symbol)

    The type of content to be accessed

  • access (Symbol)

    Read/write access of the specified type

  • description (String)

    Human readable description of what the permission will be used for



193
194
195
196
197
# File 'lib/diaspora-client.rb', line 193

def self.permission(type, access, description)
  @permissions[type] = {:type => "DiasporaClient::#{type.to_s.upcase}".constantize,
                        :access => "DiasporaClient::#{access.to_s.upcase}".constantize,
                        :description => description}
end

+ (OpenSSL::PKey::RSA) private_key

Retreive the application's private key.

Returns:

  • (OpenSSL::PKey::RSA)

    Application's private key



103
104
105
# File 'lib/diaspora-client.rb', line 103

def self.private_key
  @private_key ||= OpenSSL::PKey::RSA.new(File.read(@private_key_path))
end

+ (String) public_key

Retreive the application's public key.

Returns:

  • (String)

    Application's public key



96
97
98
# File 'lib/diaspora-client.rb', line 96

def self.public_key
  @public_key ||= File.read(@public_key_path)
end

+ (String) scheme

Application's current protocol (http/https). The test_mode configuration flag will turn SSL off.

Returns:

  • (String)

    Protocol



89
90
91
# File 'lib/diaspora-client.rb', line 89

def self.scheme
  @test_mode ? 'http' : 'https'
end

+ (void) set_test_defaults

This method returns an undefined value.

Sets default config values for testing



216
217
218
# File 'lib/diaspora-client.rb', line 216

def self.set_test_defaults
  @application_base_url ||= "example.com"
end

+ (void) setter(*fields)

This method returns an undefined value.

Sets setter field(s) for the module.

Parameters:

  • fields (Symbol, String)

    Variables to be set



24
25
26
27
28
# File 'lib/diaspora-client.rb', line 24

def self.setter(*fields)
  fields.each do |f|
    eval("def self.#{f}=(val) ; @#{f} = val ; end")
  end
end

+ (void) setup_faraday

This method returns an undefined value.

Configures Faraday for JSON requests.



127
128
129
130
131
132
# File 'lib/diaspora-client.rb', line 127

def self.setup_faraday
  @faraday_initialized ||= Faraday.default_connection = Faraday::Connection.new do |builder|
    builder.use Faraday::Request::JSON
    builder.adapter self.which_faraday_adapter?
  end
end

+ (String) sign(plaintext)

A SHA256 signature of the passed in plaintext with the private_key

Returns:

  • (String)

    A SHA256 signature of the passed in plaintext with the private_key



108
109
110
# File 'lib/diaspora-client.rb', line 108

def self.sign(plaintext)
  self.private_key.sign(OpenSSL::Digest::SHA256.new, plaintext)
end

+ (Symbol) which_faraday_adapter?

Returns either :em_synchrony or :net_http for Faraday according to if the application is running in an EventMachine reactor loop.

Returns:

  • (Symbol)

    The Faraday adapter.



116
117
118
119
120
121
122
# File 'lib/diaspora-client.rb', line 116

def self.which_faraday_adapter?
  if(defined?(EM::Synchrony) && EM.reactor_running?)
    :em_synchrony
  else
    :net_http
  end
end