Class: TentClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tent-client.rb,
lib/tent-client/post.rb,
lib/tent-client/version.rb,
lib/tent-client/discovery.rb,
lib/tent-client/tent_type.rb,
lib/tent-client/attachment.rb,
lib/tent-client/cycle_http.rb,
lib/tent-client/link_header.rb,
lib/tent-client/middleware/encode_json.rb,
lib/tent-client/middleware/authentication.rb,
lib/tent-client/middleware/content_type_header.rb

Defined Under Namespace

Modules: Middleware Classes: Attachment, CycleHTTP, Discovery, LinkHeader, Post, TentType

Constant Summary collapse

POST_MEDIA_TYPE =
%(application/vnd.tent.post.v0+json).freeze
POST_CONTENT_TYPE =
%(#{POST_MEDIA_TYPE}; type="%s").freeze
POST_MENTIONS_CONTENT_TYPE =
%(application/vnd.tent.post-mentions.v0+json).freeze
POST_VERSIONS_CONTENT_TYPE =
%(application/vnd.tent.post-versions.v0+json).freeze
POST_CHILDREN_CONTENT_TYPE =
%(application/vnd.tent.post-children.v0+json).freeze
OAUTH_TOKEN_CONTENT_TYPE =
%(application/vnd.tent.oauth.token.v0+json).freeze
MULTIPART_CONTENT_TYPE =
'multipart/form-data'.freeze
MULTIPART_BOUNDARY =
"-----------TentPart".freeze
MalformedServerMeta =
Class.new(StandardError)
ServerNotFound =
Class.new(StandardError)
VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_uri, options = {}) ⇒ TentClient

Returns a new instance of TentClient.



47
48
49
50
51
52
53
# File 'lib/tent-client.rb', line 47

def initialize(entity_uri, options = {})
  @server_meta_post = options.delete(:server_meta)
  @faraday_adapter = options.delete(:faraday_adapter)
  @faraday_setup = options.delete(:faraday_setup)
  @ts_skew = options.delete(:ts_skew)
  @entity_uri, @options = entity_uri, options
end

Instance Attribute Details

#entity_uriObject (readonly)

Returns the value of attribute entity_uri.



44
45
46
# File 'lib/tent-client.rb', line 44

def entity_uri
  @entity_uri
end

#faraday_adapterObject



97
98
99
# File 'lib/tent-client.rb', line 97

def faraday_adapter
  @faraday_adapter || Faraday.default_adapter
end

#faraday_setup=(value) ⇒ Object (writeonly)

Sets the attribute faraday_setup

Parameters:

  • value

    the value to set the attribute faraday_setup to.



45
46
47
# File 'lib/tent-client.rb', line 45

def faraday_setup=(value)
  @faraday_setup = value
end

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/tent-client.rb', line 44

def options
  @options
end

#server_meta_postObject



67
68
69
# File 'lib/tent-client.rb', line 67

def server_meta_post
  @server_meta_post ||= entity_uri ? Discovery.discover(self, entity_uri) : nil
end

#ts_skewObject

Returns the value of attribute ts_skew.



46
47
48
# File 'lib/tent-client.rb', line 46

def ts_skew
  @ts_skew
end

Instance Method Details

#attachmentObject



118
119
120
# File 'lib/tent-client.rb', line 118

def attachment
  Attachment.new(self)
end

#dupObject



55
56
57
58
59
60
61
# File 'lib/tent-client.rb', line 55

def dup
  self.class.new(@entity_uri, @options.merge(
    :server_meta => @server_meta_post,
    :faraday_adapter => @faraday_adapter,
    :faraday_setup => @faraday_setup
  ))
end

#hex_digest(data) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/tent-client.rb', line 105

def hex_digest(data)
  if data.kind_of?(IO)
    _data = data.read
    data.rewind
    data = _data
  end
  Digest::SHA512.new.update(data).to_s[0...64]
end

#httpObject



93
94
95
# File 'lib/tent-client.rb', line 93

def http
  @http || new_http
end

#new_httpObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tent-client.rb', line 75

def new_http
  authentication_options = {}
  authentication_options[:ts_skew] = @ts_skew if @ts_skew
  authentication_options[:ts_skew_retry_enabled] = @options.has_key?(:ts_skew_retry_enabled) ? @options[:ts_skew_retry_enabled] : true
  authentication_options[:update_ts_skew] = proc do |skew|
    @ts_skew = skew
  end

  @http = CycleHTTP.new(self) do |f|
    f.use Middleware::ContentTypeHeader
    f.use Middleware::EncodeJson unless @options[:skip_serialization]
    f.use Middleware::Authentication, @options[:credentials], authentication_options if @options[:credentials]
    f.response :multi_json, :content_type => /\bjson\Z/ unless @options[:skip_serialization] || @options[:skip_response_serialization]
    @faraday_setup.call(f) if @faraday_setup
    f.adapter *Array(faraday_adapter)
  end
end

#oauth_redirect_uri(params = {}) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/tent-client.rb', line 122

def oauth_redirect_uri(params = {})
  uri = URI(primary_server['urls']['oauth_auth'])

  query = params.inject([]) { |m, (k,v)| m << "#{k}=#{URI.encode_www_form_component(v)}"; m }.join('&')
  uri.query ? uri.query += "&#{query}" : uri.query = query

  uri
end

#oauth_token_exchange(data, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/tent-client.rb', line 131

def oauth_token_exchange(data, &block)
  new_block = proc do |request|
    request.headers['Content-Type'] = OAUTH_TOKEN_CONTENT_TYPE
    yield(request) if block_given?
  end
  http.post(:oauth_token, params = {}, {
    :token_type => 'https://tent.io/oauth/hawk-token'
  }.merge(data), &new_block)
end

#postObject



114
115
116
# File 'lib/tent-client.rb', line 114

def post
  Post.new(self)
end

#primary_serverObject



71
72
73
# File 'lib/tent-client.rb', line 71

def primary_server
  server_meta['servers'].sort_by { |s| s['preference'] }.first
end

#server_metaObject



63
64
65
# File 'lib/tent-client.rb', line 63

def server_meta
  server_meta_post['content'] if server_meta_post
end