Class: ICMClient::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/icm_ruby_client.rb

Constant Summary collapse

DEFAULT_LIMIT =

The default limit for list/pagination

100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, client_options = {}, base_url = 'https://api.icmobile.singlewire.com/api/v1-DEV', path = nil, resource = nil) ⇒ Client

Creates a new instance of an InformaCast Mobile REST client Params:

access_token

Required argument for interacting with the REST API

client_options

Optional argument for supplying additional options to RestClient.

base_url

Optional argument for overriding the default base base_url. Useful for testing.

path

Optional argument for overriding the base path.

resource

Optional argument for overriding the default RestClient.



25
26
27
28
29
30
31
32
33
# File 'lib/icm_ruby_client.rb', line 25

def initialize(access_token, client_options={}, base_url='https://api.icmobile.singlewire.com/api/v1-DEV', path=nil, resource=nil)
  @access_token = access_token.freeze or raise ArgumentError, 'must pass :access_token'
  @base_url = base_url.freeze or raise ArgumentError, 'must pass :base_url'
  @path = path.freeze
  @resource = resource || RestClient::Resource.new(base_url, {:headers => {
                                                                     :authorization => "Bearer #{access_token}",
                                                                     :x_client_version => 'RubyClient 0.0.1'
                                                                 }}.merge(client_options || {}))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Provides the magic necessary to chain method calls

Examples

client = ICMClient::Client.new('<My Access Token>')
puts client.users('<User Id>').devices.get

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/icm_ruby_client.rb', line 39

def method_missing(symbol, *args)
  raise ArgumentError, 'only one argument may be provided' if args.length > 1
  formatted_resource_name = symbol.to_s.gsub('_', '-')
  resource_id = ("/#{args.first}" unless args.empty?)
  new_path = "#{@path}/#{formatted_resource_name}#{resource_id}"
  Client.new(@access_token, nil, @base_url, new_path, @resource)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



13
14
15
# File 'lib/icm_ruby_client.rb', line 13

def access_token
  @access_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



13
14
15
# File 'lib/icm_ruby_client.rb', line 13

def base_url
  @base_url
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/icm_ruby_client.rb', line 13

def path
  @path
end

Instance Method Details

#list(*args) ⇒ Object

Builds a lazy enumerator for paging through resources defined in the InformaCast Mobile API



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/icm_ruby_client.rb', line 48

def list(*args)
  args = [{:params => {:limit => DEFAULT_LIMIT}}] if args.empty? or !args.first.respond_to?(:has_key?)
  args.first[:params] = {:limit => DEFAULT_LIMIT} unless args.first[:params]
  args.first[:params][:limit] = DEFAULT_LIMIT unless args.first[:params][:limit]
  next_token = nil
  Enumerator.new do |y|
    while true
      args.first[:params][:start] = next_token if next_token
      raw_response_str = nested_resource.get(*args)
      response = JSON.parse(raw_response_str, :symbolize_names => true)
      resources = response[:data]
      resources.each { |resource| y.yield resource }
      next_token = response[:next]
      break unless next_token
    end
  end
end