Class: TheCity::API::Client

Inherits:
Client
  • Object
show all
Includes:
Accounts, Events, Groups, Needs, Prayers, Topics, Users
Defined in:
lib/the_city/api/client.rb

Overview

Wrapper for the TheCity REST API

Constant Summary collapse

ENDPOINT =
ENV['THECITY_API_ENDPOINT'] || 'https://api.onthecity.org'

Instance Attribute Summary collapse

Attributes inherited from Client

#access_token, #app_id, #app_secret, #subdomain, #version

Instance Method Summary collapse

Methods included from Needs

#need, #post_need

Methods included from Prayers

#post_prayer, #prayer

Methods included from Events

#event, #post_event

Methods included from Topics

#post_topic, #topic

Methods included from Groups

#group, #my_groups

Methods included from Accounts

#church_account, #my_accounts

Methods included from Users

#me, #permissions, #user, #user?

Methods inherited from Client

#credentials, #credentials?, #initialize

Constructor Details

This class inherits a constructor from TheCity::Client

Instance Attribute Details

#connection_optionsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/the_city/api/client.rb', line 40

def connection_options
  {
    :builder => middleware,
    :headers => {
      :accept => "application/vnd.thecity.v#{version}+json",
      'X-THECITY-SUBDOMAIN' => subdomain,
      'X-THECITY-ACCESS-TOKEN' => access_token,
    },
    :request => {
      :open_timeout => 5,
      :timeout => 60,
    },
  }
end

#middlewareFaraday::Builder

Note:

Faraday’s middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/the_city/api/client.rb', line 59

def middleware
  @middleware ||= Faraday::Builder.new do |builder|
    # Convert file uploads to Faraday::UploadIO objects
    builder.use TheCity::API::Request::MultipartWithFile
    # Checks for files in the payload
    builder.use Faraday::Request::Multipart
    # Convert request params to "www-form-urlencoded"
    builder.use Faraday::Request::UrlEncoded
    # Handle error responses
    builder.use TheCity::API::Response::RaiseError
    # Parse JSON response bodies
    builder.use TheCity::API::Response::ParseJson
    # Set Faraday's HTTP adapter
    builder.adapter Faraday.default_adapter
  end
end

Instance Method Details

#delete(path, params = {}) ⇒ Object

Perform an HTTP DELETE request



77
78
79
# File 'lib/the_city/api/client.rb', line 77

def delete(path, params={})
  request(:delete, path, params)
end

#get(path, params = {}) ⇒ Object

Perform an HTTP GET request



82
83
84
# File 'lib/the_city/api/client.rb', line 82

def get(path, params={})
  request(:get, path, params)
end

#post(path, params = {}) ⇒ Object

Perform an HTTP POST request



87
88
89
90
# File 'lib/the_city/api/client.rb', line 87

def post(path, params={})
  signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
  request(:post, path, params, signature_params)
end

#put(path, params = {}) ⇒ Object

Perform an HTTP PUT request



93
94
95
# File 'lib/the_city/api/client.rb', line 93

def put(path, params={})
  request(:put, path, params)
end