Class: Fixer::API

Inherits:
Object
  • Object
show all
Includes:
Configuration, Connection
Defined in:
lib/fixer/api.rb

Direct Known Subclasses

Client, Jobs

Constant Summary

Constants included from Connection

Connection::ALLOWED_OPTIONS

Constants included from Configuration

Configuration::DEFAULT_ADAPTER, Configuration::DEFAULT_ENDPOINT, Configuration::DEFAULT_QUEUE, Configuration::DEFAULT_USER_AGENT, Configuration::VALID_OPTIONS_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#connection, #get_token, #process_options

Methods included from Configuration

#configure, extended, keys, #options, #reset!

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ API

Returns a new instance of API.

Yields:

  • (_self)

Yield Parameters:

  • _self (Fixer::API)

    the object that the method was called on



22
23
24
25
# File 'lib/fixer/api.rb', line 22

def initialize(options = {})
  apply_options(options)
  yield(self) if block_given?
end

Instance Attribute Details

#current_optionsObject

Returns the value of attribute current_options.



11
12
13
# File 'lib/fixer/api.rb', line 11

def current_options
  @current_options
end

Instance Method Details

#apply_options(options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fixer/api.rb', line 27

def apply_options(options = {})
  self.current_options ||= Fixer.options.with_indifferent_access
  self.current_options = current_options.merge(args_to_options(options))
  Configuration.keys.each do |key|
    send("#{key}=", current_options[key])
  end
end

#args_to_options(args) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/fixer/api.rb', line 92

def args_to_options(args)
  params =  if args.is_a?(String) || args.is_a?(Symbol) || args.is_a?(Numeric)
    {"#{self.class.name.demodulize.downcase.singularize}_id" => args.to_s}
  elsif args.is_a?(Hash)
    args
  end
end

#base_pathObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fixer/api.rb', line 55

def base_path
  parts = self.class.name.split("::").inject([]){|a, c|
    if c != 'Fixer'
      base = c.underscore
      a << base.tr('_','-')
      a << current_options["#{base.singularize}_id"] if current_options["#{base.singularize}_id"]
    end
    a
  }
  parts.join('/') + '/'
end

#create(params = {}) ⇒ Object



77
78
79
80
# File 'lib/fixer/api.rb', line 77

def create(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:post, base_path, { data: params } )
end

#delete(params = {}) ⇒ Object



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

def delete(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:delete, base_path)
end

#get(params = {}) ⇒ Object



72
73
74
75
# File 'lib/fixer/api.rb', line 72

def get(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:get, base_path)
end

#list(params = {}) ⇒ Object



67
68
69
70
# File 'lib/fixer/api.rb', line 67

def list(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:get, base_path)
end

#request(method, path, params = {}) ⇒ Object

:nodoc:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fixer/api.rb', line 35

def request(method, path, params={}) # :nodoc:
  unless (method && [:get, :post, :put, :patch, :delete].include?(method))
    raise ArgumentError, "whoops, that isn't a valid http method: #{method}"
  end

  conn = connection((params[:options] || {}).merge(current_options))
  request_path = ('/' + conn.path_prefix + '/' + path).gsub(/\/+/, '/')

  response = conn.send(method) do |request|
    case method.to_sym
    when :get, :delete
      request.url(request_path, params)
    when :post, :put
      request.path = request_path
      request.body = params[:data]
    end
  end
  Fixer::Response.new(response, {api: self, method: method, path: path, params: params})
end

#update(params = {}) ⇒ Object



82
83
84
85
# File 'lib/fixer/api.rb', line 82

def update(params={})
  self.current_options = current_options.merge(args_to_options(params))
  request(:put, base_path, { data: params } )
end